package com.kung.qvs.ui.main.fragment;

import android.app.Application;
import android.bluetooth.BluetoothDevice;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.data.JtRepository;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class BleDeviceViewModel extends BaseViewModel<JtRepository> {
    public BleDeviceViewModel(@NonNull Application application, JtRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent finishRefreshing = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadmore = new SingleLiveEvent<>();

        //订单详情页
        public SingleLiveEvent<String> toPageRecycleDetail = new SingleLiveEvent();
        public SingleLiveEvent<Boolean> toChangeCameraState = new SingleLiveEvent();
        //
        public SingleLiveEvent toPageUp = new SingleLiveEvent();

        public SingleLiveEvent<BluetoothDevice> toConnectBle = new SingleLiveEvent<>();
        public SingleLiveEvent<BluetoothDevice> toConnectBleReal = new SingleLiveEvent<>();
    }

    //给RecyclerView添加ObservableList
    public ObservableList<BleDeviceItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<BleDeviceItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_ble_record);

    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.finishRefreshing.call();
        }
    });
    //上拉加载
    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.finishLoadmore.call();
            //模拟网络上拉加载更多

        }
    });

    public BindingCommand onCommandBack = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.toPageUp.call();
        }
    });

    public void onConnectBle(BluetoothDevice device){
        uc.toConnectBle.setValue(device);
    }

    public void addBleDevice(BluetoothDevice device){
        observableList.add(new BleDeviceItemViewModel(BleDeviceViewModel.this, device));
    }

    public void notifyDataSetChanged(){
//        observableList.notifyAll();
    }

    public void notifyItemChanged(int i){
        observableList.get(i).entity.notifyChange();
    }
}
